package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaArticleAddDTO;
import com.cheer.dto.OaArticleUpdateDTO;
import com.cheer.service.OaArticleClassService;
import com.cheer.service.OaArticleService;
import com.cheer.vo.OaArticleInfoVO;
import com.cheer.vo.OaArticleListVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/article")
public class OaArticleController {

    @Autowired
    private OaArticleService oaArticleService;

    @Autowired
    private OaArticleClassService oaArticleClassService;

    @ApiOperation("添加文章")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaArticleAddDTO articleAddDTO) {
        oaArticleService.add(articleAddDTO);
        return Result.success();
    }

    @ApiOperation("查询文章分类")
    @GetMapping("/class")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<SimpleObjectVO>> articleClass() {
        return Result.success(oaArticleClassService.listSimple());
    }

    @ApiOperation("查询文章列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaArticleListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                              @RequestParam(required = false) Integer articleClass,
                                              @RequestParam(required = false) String title,
                                              @RequestParam(required = false) Integer id) {
        return Result.success(oaArticleService.list(pageNum, pageSize, articleClass, title, id));
    }

    @ApiOperation("批量删除")
    @PostMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestBody List<Long> ids) {
        oaArticleService.delete(ids);
        return Result.success();
    }

    @ApiOperation("查询文章详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaArticleInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaArticleService.info(id));
    }

    @ApiOperation("修改文章")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaArticleUpdateDTO articleUpdateDTO) {
        oaArticleService.update(articleUpdateDTO);
        return Result.success();
    }

    @ApiOperation("启用或禁用")
    @GetMapping("/status")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        oaArticleService.updateStatus(id, status);
        return Result.success();
    }

    @ApiOperation("推荐或取消推荐")
    @GetMapping("/recommend")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateRecommend(@RequestParam Integer id, @RequestParam Integer recommend) {
        oaArticleService.updateRecommend(id, recommend);
        return Result.success();
    }

    @ApiOperation("查询文章")
    @GetMapping("/all")
    public Result<List<SimpleObjectVO>> all() {
        return Result.success(oaArticleService.all());
    }

    @ApiOperation("查询文章")
    @GetMapping("/class/all")
    public Result<List<SimpleObjectVO>> listSimpleByArticleClass(@RequestParam Integer articleClass) {
        return Result.success(oaArticleService.listSimpleByArticleClass(articleClass));
    }
}
